//
//  OrderFormModel.swift
//  MikerShop
//
//  Created by eme on 2016/12/19.
//  Copyright © 2016年 eme. All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol

class OrderFormModel: ModelProtocol {


    internal let kBaseClassFinalpriceKey: String = "finalprice"
    internal let kBaseClassFinalnumKey: String = "finalnum"
    internal let kBaseClassFinaltotalpriceKey: String = "finaltotalprice"
    internal let kBaseClassFinalposttagKey: String = "finalposttag"
    
    /// 有效商品
    var effective:[OrderProductModel]
    /// 失效商品
    var loseefficacy:[OrderProductModel]
//    是否开发票,0否,1是
    var issueaninvoice:String
    var buyNum:Int = 0
    var buyMoney:Double = 0.0
    /// 判断有效商品的实现
    var hasSelected:Bool{
        get{
            var isOk = true
            var totalNum:Int = 0
            var totalMoney:Double = 0.0
            /// 如果有效商品都为空，则显示未选中
            if effective.count == 0 {
                return false
            }

            for item in self.effective {
                if item.allHasSelected == false {
                    isOk = false
                }
                totalNum = totalNum + item.allBuyNum
                totalMoney = totalMoney + item.allBuyMoney
            }
            self.buyNum = totalNum
            self.buyMoney = totalMoney
            return isOk
        }
        set(newVal){
            for item  in self.effective {
                item.allHasSelected = newVal
            }
        }
    }
    /// 只要包含有选中的按钮就为true
    var isHsaSelected:Bool{
        get {
            let isOk = false
            if effective.count == 0 {
                return isOk
            }
            for effectiveModel  in self.effective {
                for subItem in effectiveModel.array{
                    for skuItem in subItem.array{
                        if skuItem.hasSelected {
                            return true
                        }
                    }
                }
            }
            return isOk
        }
    }
    
    
    
    
    // MARK: 属性
    var finalprice: Double
    var finalnum: Int
    var finaltotalprice: Double
    var finalposttag: Double
    var defaultaddress: AddressModel
    var myvoucherlist:[VouchersModel]

    required  init?(json: JSON) {
        
        finalprice = json[kBaseClassFinalpriceKey].doubleValue
        finalnum = json[kBaseClassFinalnumKey].intValue
        finaltotalprice = json[kBaseClassFinaltotalpriceKey].doubleValue
        finalposttag = json[kBaseClassFinalposttagKey].doubleValue
        defaultaddress = AddressModel(json: json["defaultaddress"])!
        issueaninvoice = json["issueaninvoice"].stringValue
        effective = []
        if let items = json["effective"].array {
            for item in items {
                if let skusModel = OrderProductModel(json: item){
                    effective.append(skusModel)
                }
            }
        }
        
        loseefficacy = []
        if let items = json["loseefficacy"].array {
            for item in items {
                if let skusModel = OrderProductModel(json: item){
                    loseefficacy.append(skusModel)
                }
            }
        }
        myvoucherlist = []
        if let items = json["myvoucherlist"].array {
            for item in items {
                if let voucher = VouchersModel(json: item){
                    myvoucherlist.append(voucher)
                }
            }
        }


        
    }


}

